<html>
<head>
  <title>life.py</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
Game of Life Engine
Mike Steder

Randomly generates Game of Life boards and displays them.
&quot;</font><font color="#008000">&quot;&quot;</font>

<font color="#444444"># My code:</font>
<strong>from</strong> <font color="#2040a0">engine</font> <strong>import</strong> <font color="4444FF">*</font>

<font color="#444444"># </font>
<strong>import</strong> <font color="#2040a0">time</font>
<strong>import</strong> <font color="#2040a0">string</font>
<strong>import</strong> <font color="#2040a0">random</font>

<font color="#444444"># PyGame Constants</font>
<strong>import</strong> <font color="#2040a0">pygame</font>
<strong>from</strong> <font color="#2040a0">pygame</font>.<font color="#2040a0">locals</font> <strong>import</strong> <font color="4444FF">*</font>

<font color="#444444"># for optimizing drawing:</font>
<strong>from</strong> <font color="#2040a0">pygame</font> <strong>import</strong> <font color="#2040a0">surfarray</font>

<font color="#2040a0">def</font> <font color="#2040a0">drawfield</font><font color="4444FF">(</font> <font color="#2040a0">screen</font>, <font color="#2040a0">scale_surface</font>, <font color="#2040a0">pixels</font> <font color="4444FF">)</font><font color="4444FF">:</font>
    <font color="#2040a0">surfarray</font>.<font color="#2040a0">blit_array</font><font color="4444FF">(</font> <font color="#2040a0">scale_surface</font>, <font color="#2040a0">pixels</font> <font color="4444FF">)</font>
    <font color="#2040a0">temp</font> <font color="4444FF">=</font> <font color="#2040a0">pygame</font>.<font color="#2040a0">transform</font>.<font color="#2040a0">scale</font><font color="4444FF">(</font><font color="#2040a0">scale_surface</font>, <font color="#2040a0">screen</font>.<font color="#2040a0">get_size</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">)</font>
    <font color="#2040a0">screen</font>.<font color="#2040a0">blit</font><font color="4444FF">(</font><font color="#2040a0">temp</font>, <font color="4444FF">(</font><font color="#FF0000">0</font>,<font color="#FF0000">0</font><font color="4444FF">)</font><font color="4444FF">)</font>
    <strong>return</strong>

<font color="#2040a0">def</font> <font color="#2040a0">writeParametersToLog</font><font color="4444FF">(</font> <font color="#2040a0">xsize</font>, <font color="#2040a0">ysize</font>, <font color="#2040a0">starting_pop</font>, <font color="#2040a0">static</font>, <font color="#2040a0">avgpop</font> <font color="4444FF">)</font><font color="4444FF">:</font>
    <strong>pass</strong>

<font color="#2040a0">def</font> <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">:</font>
    <strong>print</strong> <font color="#008000">'The game of life!'</font>
    <font color="#2040a0">WINSIZE</font> <font color="4444FF">=</font> <font color="#FF0000">640</font>,<font color="#FF0000">480</font>
    <font color="#2040a0">SCALE</font> <font color="4444FF">=</font> <font color="#FF0000">4</font>
    <font color="#2040a0">ARRAYSIZE</font> <font color="4444FF">=</font> <font color="#2040a0">WINSIZE</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font>/<font color="#2040a0">SCALE</font>, <font color="#2040a0">WINSIZE</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font>/<font color="#2040a0">SCALE</font>
    
    <font color="#444444"># Initialize the Pygame Engine!</font>
    <font color="#2040a0">pygame</font>.<font color="#2040a0">init</font><font color="4444FF">(</font><font color="4444FF">)</font>
    <font color="#2040a0">screen</font> <font color="4444FF">=</font> <font color="#2040a0">pygame</font>.<font color="#2040a0">display</font>.<font color="#2040a0">set_mode</font><font color="4444FF">(</font><font color="#2040a0">WINSIZE</font>,<font color="#FF0000">0</font>,<font color="#FF0000">8</font><font color="4444FF">)</font>
    <font color="#2040a0">scale_screen</font> <font color="4444FF">=</font> <font color="#2040a0">pygame</font>.<font color="#2040a0">surface</font>.<font color="#2040a0">Surface</font><font color="4444FF">(</font> <font color="#2040a0">ARRAYSIZE</font>,<font color="#FF0000">0</font>,<font color="#FF0000">8</font> <font color="4444FF">)</font>
    <font color="#2040a0">pygame</font>.<font color="#2040a0">display</font>.<font color="#2040a0">set_caption</font><font color="4444FF">(</font><font color="#008000">'Life'</font><font color="4444FF">)</font>
    <font color="#2040a0">white</font> <font color="4444FF">=</font> <font color="#FF0000">255</font>,<font color="#FF0000">240</font>,<font color="#FF0000">200</font>
    <font color="#2040a0">black</font> <font color="4444FF">=</font> <font color="#FF0000">20</font>,<font color="#FF0000">20</font>, <font color="#FF0000">40</font>
    <font color="#2040a0">red</font> <font color="4444FF">=</font> <font color="#FF0000">255</font>, <font color="#FF0000">20</font>, <font color="#FF0000">40</font>
    <font color="#2040a0">green</font> <font color="4444FF">=</font> <font color="#FF0000">20</font>,<font color="#FF0000">255</font>,<font color="#FF0000">40</font>
    <font color="#2040a0">blue</font> <font color="4444FF">=</font> <font color="#FF0000">20</font>,<font color="#FF0000">20</font>,<font color="#FF0000">255</font>
    <font color="#2040a0">screen</font>.<font color="#2040a0">fill</font><font color="4444FF">(</font><font color="#2040a0">black</font><font color="4444FF">)</font>
    <font color="#2040a0">scale_screen</font>.<font color="#2040a0">fill</font><font color="4444FF">(</font><font color="#2040a0">black</font><font color="4444FF">)</font>
    <font color="#2040a0">screen</font>.<font color="#2040a0">set_palette</font><font color="4444FF">(</font> <font color="4444FF">[</font><font color="#2040a0">black</font>, <font color="#2040a0">red</font>, <font color="#2040a0">green</font>, <font color="#2040a0">blue</font>, <font color="#2040a0">white</font><font color="4444FF">]</font> <font color="4444FF">)</font>
    <font color="#2040a0">scale_screen</font>.<font color="#2040a0">set_palette</font><font color="4444FF">(</font> <font color="4444FF">[</font><font color="#2040a0">black</font>, <font color="#2040a0">red</font>, <font color="#2040a0">green</font>, <font color="#2040a0">blue</font>, <font color="#2040a0">white</font><font color="4444FF">]</font> <font color="4444FF">)</font>
    
    <font color="#444444"># Initialize the model:</font>
    <font color="#444444"># 2,3,1,0 correspond to palette colors set above (0=black, 1=red, etc)</font>
    <font color="#2040a0">count</font> <font color="4444FF">=</font> <font color="#FF0000">1000</font>
    <font color="#2040a0">static</font> <font color="4444FF">=</font> <font color="#FF0000">2</font>
    <font color="#2040a0">model</font> <font color="4444FF">=</font> <font color="#2040a0">Simulation</font><font color="4444FF">(</font><font color="#2040a0">ARRAYSIZE</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font>,<font color="#2040a0">ARRAYSIZE</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font>,
                                          <font color="#FF0000">2</font>,<font color="#FF0000">3</font>,<font color="#FF0000">1</font>,<font color="#FF0000">0</font>,
                                          <font color="#2040a0">count</font>,
                                          <font color="#2040a0">static</font>, 
                                          <font color="4444FF">)</font>

    <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
    <font color="#2040a0">pop</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
    <font color="#2040a0">popsum</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
    <font color="#2040a0">popave</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
    <font color="#2040a0">timesteps</font> <font color="4444FF">=</font> <font color="#FF0000">365</font>
    <font color="#2040a0">elapsedtime</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
    <font color="#2040a0">drawtime</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
    <font color="#2040a0">updatetime</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
    <font color="#2040a0">totaltime</font><font color="4444FF">=</font><font color="#FF0000">0.0</font>
    <font color="#2040a0">fps</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
    <font color="#2040a0">sumfps</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
    <font color="#2040a0">avgfps</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
    <font color="#2040a0">done</font> <font color="4444FF">=</font> <font color="#2040a0">False</font>
    <strong>while</strong> <strong>not</strong> <font color="#2040a0">done</font><font color="4444FF">:</font>
        <strong>print</strong> <font color="#2040a0">i</font>,<font color="#008000">'-'</font>,<font color="#2040a0">avgfps</font>,<font color="#008000">'-'</font>,<font color="#2040a0">pop</font>,<font color="#008000">'-'</font>,<font color="#2040a0">popave</font>
        
        <font color="#2040a0">starttime</font> <font color="4444FF">=</font> <font color="#2040a0">time</font>.<font color="#2040a0">time</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">drawfield</font><font color="4444FF">(</font> <font color="#2040a0">screen</font>, <font color="#2040a0">scale_screen</font>, <font color="#2040a0">model</font>.<font color="#2040a0">pixel_data</font> <font color="4444FF">)</font> 
        <font color="#2040a0">endtime</font> <font color="4444FF">=</font> <font color="#2040a0">time</font>.<font color="#2040a0">time</font><font color="4444FF">(</font><font color="4444FF">)</font>
        
        <font color="#2040a0">drawtime</font> <font color="4444FF">=</font> <font color="#2040a0">endtime</font> <font color="4444FF">-</font> <font color="#2040a0">starttime</font>
        <font color="#2040a0">starttime</font> <font color="4444FF">=</font> <font color="#2040a0">time</font>.<font color="#2040a0">time</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">pygame</font>.<font color="#2040a0">display</font>.<font color="#2040a0">update</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">endtime</font> <font color="4444FF">=</font> <font color="#2040a0">time</font>.<font color="#2040a0">time</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">updatetime</font> <font color="4444FF">=</font> <font color="#2040a0">endtime</font> <font color="4444FF">-</font> <font color="#2040a0">starttime</font>
        
        <font color="#2040a0">starttime</font> <font color="4444FF">=</font> <font color="#2040a0">time</font>.<font color="#2040a0">time</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">model</font>.<font color="#2040a0">timestep</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">endtime</font> <font color="4444FF">=</font> <font color="#2040a0">time</font>.<font color="#2040a0">time</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">elapsedtime</font> <font color="4444FF">=</font> <font color="#2040a0">endtime</font><font color="4444FF">-</font><font color="#2040a0">starttime</font>
        <font color="#2040a0">totaltime</font> <font color="4444FF">=</font> <font color="4444FF">(</font><font color="#2040a0">drawtime</font> <font color="4444FF">+</font> <font color="#2040a0">updatetime</font> <font color="4444FF">+</font> <font color="#2040a0">elapsedtime</font><font color="4444FF">)</font>
        
        <font color="#2040a0">sumfps</font> <font color="4444FF">+</font><font color="4444FF">=</font> <font color="4444FF">(</font> <font color="#FF0000">1.0</font>/<font color="#2040a0">totaltime</font> <font color="4444FF">)</font>
        <font color="#2040a0">i</font> <font color="4444FF">+</font><font color="4444FF">=</font> <font color="#FF0000">1</font>    
        <font color="#2040a0">avgfps</font> <font color="4444FF">=</font>  <font color="#2040a0">sumfps</font> / <font color="#2040a0">i</font>

        <font color="#2040a0">pop</font> <font color="4444FF">=</font> <font color="#2040a0">model</font>.<font color="#2040a0">numcreatures</font> 
        <font color="#2040a0">popsum</font> <font color="4444FF">+</font><font color="4444FF">=</font> <font color="#2040a0">pop</font>
        <font color="#2040a0">prev_popave</font> <font color="4444FF">=</font> <font color="#2040a0">popave</font>
        <font color="#2040a0">popave</font> <font color="4444FF">=</font> <font color="#2040a0">popsum</font> / <font color="4444FF">(</font><font color="#2040a0">i</font><font color="4444FF">*</font><font color="#FF0000">1.0</font><font color="4444FF">)</font>
        <font color="#444444"># Restart if either all creatures die or the screen is filled:</font>
        <strong>if</strong><font color="4444FF">(</font> <font color="#2040a0">pop</font> <font color="4444FF">&lt;</font><font color="4444FF">=</font> <font color="#FF0000">0</font> <strong>or</strong>
             <font color="#2040a0">pop</font> <font color="4444FF">&gt;</font><font color="4444FF">=</font> <font color="4444FF">(</font><font color="#2040a0">ARRAYSIZE</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font> <font color="4444FF">*</font> <font color="#2040a0">ARRAYSIZE</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font><font color="4444FF">)</font> <font color="4444FF">)</font><font color="4444FF">:</font>
            <strong>print</strong> <font color="#008000">&quot;Restarting @ pop = %s&quot;</font><font color="4444FF">%</font><font color="#2040a0">pop</font>
            <font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
            <font color="#2040a0">pop</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
            <font color="#2040a0">popsum</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
            <font color="#2040a0">popave</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
            <font color="#2040a0">sumfps</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>
            <font color="#2040a0">model</font> <font color="4444FF">=</font> <font color="#2040a0">Simulation</font><font color="4444FF">(</font><font color="#2040a0">ARRAYSIZE</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font>,<font color="#2040a0">ARRAYSIZE</font><font color="4444FF">[</font><font color="#FF0000">1</font><font color="4444FF">]</font>,
                                          <font color="#FF0000">2</font>,<font color="#FF0000">3</font>,<font color="#FF0000">1</font>,<font color="#FF0000">0</font>,
                                          <font color="#2040a0">random</font>.<font color="#2040a0">randint</font><font color="4444FF">(</font><font color="#FF0000">100</font>,<font color="#FF0000">3000</font><font color="4444FF">)</font>,
                                          <font color="#2040a0">random</font>.<font color="#2040a0">randint</font><font color="4444FF">(</font><font color="#FF0000">1</font>,<font color="#FF0000">7</font><font color="4444FF">)</font>, 
                                          <font color="4444FF">)</font>
        
        <font color="#444444"># Handle someone closing the window or pressing escape</font>
        <font color="#2040a0">events</font> <font color="4444FF">=</font> <font color="#2040a0">pygame</font>.<font color="#2040a0">event</font>.<font color="#2040a0">get</font><font color="4444FF">(</font> <font color="4444FF">)</font>
        <strong>for</strong> <font color="#2040a0">e</font> <strong>in</strong> <font color="#2040a0">events</font><font color="4444FF">:</font>
            <strong>if</strong><font color="4444FF">(</font> <font color="#2040a0">e</font>.<font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">QUIT</font> <font color="4444FF">)</font><font color="4444FF">:</font>
                <font color="#2040a0">done</font> <font color="4444FF">=</font> <font color="#2040a0">True</font>
                <strong>break</strong>
            <strong>elif</strong> <font color="4444FF">(</font><font color="#2040a0">e</font>.<font color="#2040a0">type</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">KEYDOWN</font><font color="4444FF">)</font><font color="4444FF">:</font>
                <strong>if</strong><font color="4444FF">(</font> <font color="#2040a0">e</font>.<font color="#2040a0">key</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">K_ESCAPE</font> <font color="4444FF">)</font><font color="4444FF">:</font>
                    <font color="#2040a0">done</font> <font color="4444FF">=</font> <font color="#2040a0">True</font>
                    <strong>break</strong>
                <strong>if</strong><font color="4444FF">(</font> <font color="#2040a0">e</font>.<font color="#2040a0">key</font> <font color="4444FF">=</font><font color="4444FF">=</font> <font color="#2040a0">K_f</font> <font color="4444FF">)</font><font color="4444FF">:</font>
                    <font color="#2040a0">pygame</font>.<font color="#2040a0">display</font>.<font color="#2040a0">toggle_fullscreen</font><font color="4444FF">(</font><font color="4444FF">)</font>

    <strong>print</strong> <font color="#008000">&quot;Exiting!&quot;</font>

    <strong>return</strong>
<strong>if</strong> <font color="#2040a0">__name__</font><font color="4444FF">=</font><font color="4444FF">=</font><font color="#008000">&quot;__main__&quot;</font><font color="4444FF">:</font>
    <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font>
    

</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
